package com.music.mysongs.helper

import android.content.Context
import android.net.ConnectivityManager
import android.net.Network
import android.os.Build
import androidx.annotation.RequiresApi
import com.music.mysongs.utils.MyApplication
import com.music.mysongs.utils.myLog
import java.text.MessageFormat.format
import java.text.SimpleDateFormat
import java.util.*

@RequiresApi(Build.VERSION_CODES.M)
fun getNetWork(): Network? {
    val connectionManager =
        MyApplication.context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    return connectionManager.activeNetwork
}

fun getTimesFormat(addTime: Int): String {
    val sf = SimpleDateFormat("yyyy 年 MM 月 dd 日 HH 时")
    return sf.format(Date(addTime * 1000L))
}

//时长转换
fun getTimeFormat(currentPosition: Int): String? {
    try {
        val time = currentPosition / 1000
        return if (time >= 3600) {
            val hour = time / 3600
            val minute = time / 60 % 60
            val second = time % 60
            format("%02d:%02d:%02d", hour, minute, second)
        } else {
            val minute = time / 60
            val second = time % 60
            val fm = format("%02d:%02d")
            fm.format(minute, second)
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
    return null
}

//获取当前时间
fun getNow(): String {
    return if (Build.VERSION.SDK_INT >= 23) {
        SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())
    } else {
        val time = Calendar.getInstance()
        time.get(Calendar.YEAR).toString() + "-" + time.get(Calendar.MONTH)
            .toString() + "-" + time.get(Calendar.DAY_OF_MONTH)
            .toString() + " " + time.get(Calendar.HOUR_OF_DAY).toString() + ":" + time.get(
            Calendar.MINUTE
        )
            .toString() + ":" + time.get(Calendar.SECOND)
            .toString() + "." + time.get(Calendar.MILLISECOND).toString()
    }
}

//对比时间
fun getTimeMatch(new: String, old: String, days: Int): Boolean {
    if (Build.VERSION.SDK_INT >= 23) {
        val df = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        try {
            val between = df.parse(new).time - df.parse(old).time
            val match = between / (24 * 60 * 60 * 1000)
            myLog("getTimeMatch=$match")
            return match > days
        } catch (e: Exception) {
            e.printStackTrace()
        }
    } else {
        return false
    }
    return false
}

//获得本周日24点时间
fun getTimesWeeknight(): Boolean {
    val cal = Calendar.getInstance()
    cal.set(
        cal.get(Calendar.YEAR),
        cal.get(Calendar.MONDAY),
        cal.get(Calendar.DAY_OF_MONTH),
        0,
        0,
        0
    )
    cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY)
    val cur = System.currentTimeMillis()
    val test = (cal.time.time + (7 * 24 * 60 * 60 * 1000)) / 1000
    myLog("cal=$cal testWeek=$test cur=$cur")
    return cur > test
}

fun getDateNode(): String {
    val cal = Calendar.getInstance()
    return when (cal.get(Calendar.HOUR_OF_DAY)) {
        in 5..11 -> {
            "早上好"
        }
        in 12..17 -> {
            "下午好"
        }
        else -> {
            "晚上好"
        }
    }
}